<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/2
 * Time: 10:36
 */

namespace app\admin\controller;


use app\reuse\controller\ResponseJson;
use think\App;
use think\Controller;
use think\Db;

class Voucher extends Common
{
    use ResponseJson;

    function __construct()
    {
        parent::__construct();
        // 登录校验
        $this->IsLogin();
    }
    /*
     * @param null
     * @content 卡券列表
     * @return array
     * */
    function index()
    {
        if(input("get.active") == "ajax"){
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            $info = Db::table('voucher')
                ->paginate($listRows, false, ['query' => request()->param()]);
            # 返回数据
            return $this->jsonSuccessData($info);
        }else{
            #查询卡券兑换金币比列
            $amount= Db::table("amount")->find();
            $this->assign("amount",$amount['proportion']*100);
            $this->assign("amount_id",$amount['id']);
            return $this->fetch();

        }
    }
    /*
    * @param null
    * @content 卡券创建
    * @return array
    * */
    function voucher_add()
    {
        if(input("get.active")=="ajax"){
            $info = input("post.");
            unset($info['file']);
            if(!empty($info)){
                #执行添加操作
                $result= Db::table("voucher")->insert($info);
                if($result){
                    # 返回数据
//                    return $this->jsonSuccessData('',true);
                    return array("status"=>true,'msg'=>'提交成功');

                }else{
                    return array("status"=>false,'msg'=>'提交失败');
                }
            }else{
                return array("status"=>false,'msg'=>'没有接收到参数');
            }
        }else{
            return $this->fetch();
        }

    }
    /*
     * @param null
     * @content 卡券修改
     * @return array
     * */
    function voucher_write()
    {
        if(input("get.active")=="ajax"){
            $id = input("post.id");
            $info = input("post.");
            unset($info['file']);
            if(!empty($info)){
                #执行添加操作
                $result= Db::table("voucher")->where("id = $id")->update($info);
                if($result){
                    # 返回数据
//                    return $this->jsonSuccessData('',true);
                    return array("status"=>true,'msg'=>'提交成功');

                }else{
                    return array("status"=>false,'msg'=>'提交失败');
                }
            }else{
                return array("status"=>false,'msg'=>'没有接收到参数');

            }

        }else{
            $id= input("get.id");
            $voucher = Db::table("voucher")->where("id= $id")->find();
            $this->assign("id",$id);
            $this->assign("voucher",$voucher);
            return $this->fetch();
        }

    }
    /*
     * @param null
     * @content 卡券删除
     * @return array
     * */
    function voucher_del(){
        #删除的id
        $id = input("post.id");
        #删除标识
        if(!empty($id)){
        #删除
        Db::table('voucher')->where("id = $id")->delete();
        return array("status"=>true);
        }
    }
    /*
     * @param null
     * @content 卡券兑换比例修改
     * @return array
     * */
    function  voucher_amount(){
        $amount_id = input("get.amount_id");
        $info = input("post.");
        if(!empty($amount_id)){
            Db::table("amount")->where("id = $amount_id")->update($info);
            $proportion = $info['proportion']*100;
            return array("status"=>true,"proportion"=>$proportion);
        }
    }
}